package com.yang.springcloud.controller;

import com.yang.springcloud.config.JwtUtils;
import com.yang.springcloud.pojo.Acount;
import com.yang.springcloud.pojo.Student;
import com.yang.springcloud.service.StudentClientService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.naming.AuthenticationException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class StudentConsumerConterller {

//    @Autowired
//    private RestTemplate rte;

//    private static final String REST_URL_PREFIX = "http://127.0.0.1:7071";
//    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-STUDENT";

    //@Qualifier("StudentClientService")

    @Autowired
    private StudentClientService sc;

    @Autowired
    private  JwtUtils jwtUtils;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Acount user) {
// 1. 验证用户名和密码
// 2. 验证成功生成token
        System.out.println(user.getUsername() + "  " + user.getPassword());
        if (user.getUsername().equals("yangliang") && user.getPassword().equals("yangliang123")){
            String token = jwtUtils.generateToken(user);
            Map<String, Object> msg = new HashMap<String,Object>();
            msg.put("massage", "登录成功");
            msg.put("code", 200);
            msg.put("token", token);
            return msg;
        }

        Map<String, Object> msg = new HashMap<String,Object>();
        msg.put("massage", "登录失败,账号或密码错误");
        msg.put("code", 400);
        msg.put("token", null);
        return msg;
    }

    @PostMapping("/consumer/student/insert")
    public Map<String, Object> addStudent(Student s){
        System.out.println(s.getId()+ " " + s.getName() + " " + s.getBirth() + " " + s.getSex());
        //return rte.postForObject(REST_URL_PREFIX+"/insert", s, Map.class);
        return this.sc.addStudent(s);
    }

    @PostMapping("/consumer/student/delete")
    public Map<String, Object> deleteStudent(String name){
        System.out.println(name);
        //return rte.postForObject(REST_URL_PREFIX+"/delete", name, Map.class);
        return this.sc.delereStudent(name);

    }

    @PostMapping("/consumer/student/update")
    public Map<String, Object> updateStudent(Student s){
        //return rte.postForObject(REST_URL_PREFIX+"/update", s, Map.class);
        return this.sc.updateStudent(s);
    }

    @GetMapping("/consumer/student/query")
    public List<Student> queryStudent(@RequestHeader("token") String token, String name) throws AuthenticationException {

        List<String> blacklistToken = Arrays.asList("禁止访问的token");
        Claims claims = jwtUtils.getClaimByToken(token);
        if (claims == null || jwtUtils.isTokenExpired(claims.getExpiration()) || blacklistToken.contains(token)) {
            throw new AuthenticationException("token 不可用");
        }

        //return rte.getForObject(REST_URL_PREFIX+"/query",List.class);
        return this.sc.queryAll();
    }
}
